<?php
require_once('HTTP/Upload.php');
class FRM_Tools_Upload {
	static function Upload($dir, $sizes){
		global $_FILES;
		$names = array();
		$upload = new HTTP_Upload('en');
		$files = $upload->getFiles();


		$i = 0;
		foreach($files as $k => $file){
			if (PEAR::isError($file)) {
				echo $file->getMessage();
			}
			if ($file->isValid()) {

				$ph_name = $file->setName('uniq');
				$prop = $file->getProp();

				$image = $prop['name'];
				$dest_dir = PATH_BASE . "/$dir/orig/";
				//print $dest_dir;
				$dest_name = $file->moveTo($dest_dir);
				if (PEAR::isError($dest_name)) {
					die($dest_name->getMessage());
				}

				if (!empty($image)) {
					foreach ($sizes as $size){
						FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/{$size['folder']}/" . $image, $size['x'], $size['y']);
					}
					$names[] = $image;
				}

			}
		}
		return $names;
	}
			
	static function UploadAdmin($fields, $sessionKey = 'admin_images'){
		global $_SESSION;
		$names = array();
		$upload = new HTTP_Upload('en');
		$files = $upload->getFiles();

		//$log = new Admin_MyLog();

		//$log->insert(array('text'=>Zend_Json_Encoder::encode($_FILES)));

		$i = 0;
		foreach($files as $k => $file){
			if (PEAR::isError($file)) {
				echo $file->getMessage();
			}

			if ($file->isValid()) {

				$dir = '';
				$sizes = array();
				$use_array = false;
				//$m= new Admin_MyLog();
				//$m->insert(array('text'=>$k));
				if(strpos($k, '[0]')){
					$use_array = true;
					$k = str_replace('[0]', '', $k);
				}
				foreach ($fields as $v){
					if(($v['name'] === $k || $v['name'] === $k.'[]') && !empty($v['type'])){
						$dir = $v['dir'];
						$sizes = $v['setting'];
						break;
					}
				}

				$ph_name = $file->setName('uniq');
				$prop = $file->getProp();

				$image = $prop['name'];
				$dest_dir = PATH_BASE . "/$dir/orig/";
				//print $dest_dir;
				$dest_name = $file->moveTo($dest_dir);
				if (PEAR::isError($dest_name)) {
					die($dest_name->getMessage());
				}

				if (!empty($image)) {
					foreach ($sizes as $size){
						FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/{$size['folder']}/" . $image, $size['x'], $size['y']);
					}

					if($use_array){
						if(!isset($_SESSION[$sessionKey][$k])){
							$_SESSION[$sessionKey][$k] = array();
						}
						$_SESSION[$sessionKey][$k][]= $image;
					}else {
						$_SESSION[$sessionKey][$k]= $image;
					}
					//$m->insert(array('text'=>Zend_Json_Encoder::encode($_SESSION).Zend_Json_Encoder::encode($_COOKIE)));
					$names[] = $image;
				}

			}
		}
		return $names;
	}
	
	//-------
	
	static function UploadFile($pname,$dir, $sizes){
		global $_FILES;
		error_reporting(0);
		$names = array();
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles();


		$i = 0;
		foreach($files as $k => $file){
			if (PEAR::isError($file)) {
				echo $file->getMessage();
			}
			if ($file->isValid()) {

				$ph_name = $file->setName('uniq');
				$prop = $file->getProp();
				
				if ($prop['form_name'] == $pname) {
				$image = $prop['name'];
				$dest_dir = $dir;
					}
					else
						continue;				
				
				$dest_dir = PATH_BASE . "/$dir/orig/";
				//print $dest_dir;
				$dest_name = $file->moveTo($dest_dir);
				if (PEAR::isError($dest_name)) {
					die($dest_name->getMessage());
				}

				if (!empty($image)) {
					foreach ($sizes as $size){
						FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/{$size['folder']}/" . $image, $size['x'], $size['y']);
					}
					
				}

			}
		}
		return $image;
	}
	
	static function UploadImages($dir){
		global $_FILES;
		$names = array();
		$upload = new HTTP_Upload('en');
		$files = $upload->getFiles();
		
		

		$i = 0;
		foreach($files as $k => $file){
			if (PEAR::isError($file)) {
				echo $file->getMessage();
			}
			if ($file->isValid()) {

				$ph_name = $file->setName('uniq');
				$prop = $file->getProp();

				$image = $prop['name'];
				$dest_dir = PATH_BASE . "/$dir/orig/";
				//print $dest_dir;
				$dest_name = $file->moveTo($dest_dir);
				if (PEAR::isError($dest_name)) {
					die($dest_name->getMessage());
				}

				if (!empty($image)) {
											
						switch ($prop['form_name']){
							case 'photo':
								FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/big/" . $image, 400, 300, true);
								FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/small/" . $image, 150, 110);
								break;
							case 'plan':
								FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/plan/" . $image, 400, 300, true);							
								break;
							case 'logo':
								FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/" . $image, 150, 110, false);							
								break;
							default:
								FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/big/" . $image, 400, 300, true);
								FRM_Tools_Image::Resize(PATH_BASE . "/$dir/orig/" . $image, PATH_BASE .  "/$dir/small/" . $image, 150, 110);
								break;							
						}
						
					
					$names[$prop['form_name']] = $image;
				}

			}
		}
		return $names;
	}

}
